/*
 * Created on Aug 10, 2004
 */
package com.scalar.chiptrack.adaptor.jobs;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.utils.StringUtils;

/**
 * @author Ravikanth
**/
public class TransitLotAlertJob extends JobHandler
{
	public void execute() throws Exception
    {
		/*ArrayList transitDelayedLotList = null;
		try
		{
			transitDelayedLotList = ShippedLotDAO.loadAllTransitDelayedLots();
            
			 if( ( transitDelayedLotList == null ) || ( transitDelayedLotList.size() == 0 ) ) 
			 {
				 return;
			 }
			 
			 sendAlert( transitDelayedLotList );
		 }
		 catch (Exception e) 
		 {
			 System.err.println ("Exception while running ShippingDelayedNotificationJob: " + e);
			 e.printStackTrace();
			 throw e;
		 }*/
	}

    private String createMessageBody( ArrayList transitDelayedLotList ) 
	{
		StringBuffer messageBody = new StringBuffer();
		Iterator transitDelayedLotItems = transitDelayedLotList.iterator();
		ShippedLot shippedLot = null;
		int i=0;
		String bgColor = null;
        
		// html page design -- start
        
		messageBody.append ("<html><head>\n");
		messageBody.append ("<style type=\"text/css\">");
		messageBody.append ("#toc a{ font-family: Verdana;font-style : normal ;font-size : 9pt; font-weight :bold;}");
		messageBody.append ("#toc p{font-family: Verdana;font-style : normal ;font-size : 9pt; font-weight :bold;text-align :center;color :#FFFFFF}");
		messageBody.append ("#toc {border-style : solid;border-color :#000000 ;border-width :medium }");
		messageBody.append ("</style></head><body bgcolor='FDF5E6'>");
		// table -- start
		messageBody.append("<TABLE width='100%' align=center border=0><TBODY>");
		messageBody.append("<TR><TD align=center bgColor=#993333 colSpan=2>");
		messageBody.append("<FONT color=#ffffff><FONT size=5><STRONG>Transit Delayed Lots as of ").append(StringUtils.formatDateToString (new Date (System.currentTimeMillis()))).append("</STRONG></FONT></FONT></TD></TR>");
		messageBody.append("<TR><TD colSpan=2><BR></TD></TR>");
		messageBody.append("<TR bgColor=#dee1e2><TD colSpan=2><BR>");
		messageBody.append("<TABLE cellSpacing=0 cellPadding=0 width='100%' align=center bgColor=#ffffff border=1><TBODY>");
		messageBody.append("<TR><TD><TABLE cellSpacing=3 cellPadding=2 width='100%' align=center bgColor=#ffffff border=0><TBODY>");
		messageBody.append("<TR><TD align=center colSpan=6><BR></TD></TR>");
		messageBody.append("<TR><TD colSpan=6>");
		messageBody.append("<TABLE borderColor=#000000 cellSpacing=0 cellPadding=0 width='100%' border=1 bordercolor='#000000'><TBODY><TR>");
		messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff><b>Part<br>Number</b></FONT></TD>");
		messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff><b>Lot<br>Number</b></FONT></TD>");
		messageBody.append("<TD align=center width='20%' bgColor=#993333><FONT color=#ffffff><b>Comming From</b></FONT></TD>");
		messageBody.append("<TD align=center width='20%' bgColor=#993333><FONT color=#ffffff><b>Going To</b></FONT></TD>");
		messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff><b>Shipped<br>Quantity</b></FONT></TD>");
		messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff><b>Shipped<br>Date</b></FONT></TD>");
		messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff><b>ETD*</b></FONT></TD>");
		messageBody.append("<TD align=center width='10%' bgColor=#993333><FONT color=#ffffff><b>ETA*</b></FONT></TD>");
		messageBody.append("<TD align=center width='20%' bgColor=#993333><FONT color=#ffffff><b>Comments</b></FONT></TD>");
		messageBody.append("</TR>");
                      
		while( transitDelayedLotItems.hasNext() ) 
		{
			if (i%2 == 0) bgColor = "#DEE1E2";
			else bgColor = "#FFFFEE";

			shippedLot = (ShippedLot) transitDelayedLotItems.next();

			// have to take care of the null fields, date format and Qty format.
			messageBody.append ("<tr>");
			messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getPartNumber()).append("&nbsp;</td>");
			messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getLotNumber() ).append("&nbsp;</td>");
			messageBody.append ("<TD align=left  width='20%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append( shippedLot.getCurrentLocation() != null?shippedLot.getCurrentLocation():""  ).append("&nbsp;</td>");
			messageBody.append ("<TD align=left  width='20%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append( shippedLot.getNextLocation()!= null?shippedLot.getNextLocation():"" ).append("&nbsp;</td>");
			messageBody.append ("<TD align=right  width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append(shippedLot.getShipQty()).append("&nbsp;</td>");
			messageBody.append ("<TD align=center  width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append( StringUtils.formatDate( shippedLot.getShipDate(), "yyyy-MM-dd HH:mm:ss.S", "MM/dd/yyyy" ) ).append("&nbsp;</td>");
			messageBody.append ("<TD align=center  width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append((shippedLot.getEtd()==null)?"&nbsp":StringUtils.formatDate( shippedLot.getEtd(), "MMM d yyyy HH:mm", "MM/dd/yyyy" ) ).append("&nbsp;</td>");
			messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append((shippedLot.getEta()==null)?"&nbsp":shippedLot.getEta()).append("&nbsp;</td>");
			messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append( shippedLot.getRemarks()).append("&nbsp;</td>");
			//messageBody.append ("<TD align=center  width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append((shippedLot.getEtd()==null)?"&nbsp":shippedLot.getEtd()).append("&nbsp;</td>");
			//messageBody.append ("<TD align=center width='10%' bgColor=").append( bgColor).append(" nowrap>&nbsp;").append((shippedLot.getEta()==null)?"&nbsp":shippedLot.getEta()).append("&nbsp;</td>");
			messageBody.append ("</tr>");
			i++;   
		}
        
		messageBody.append ("</TBODY></TABLE></TD></TR>");
		messageBody.append ("<TR><TD align=left colSpan=6><BR><font size=1>");
		messageBody.append ("*&nbsp;ETD : Expected Time of Delivery &nbsp;*&nbsp; ETA : Expected Time of Arrival <br>");
		messageBody.append ("</font></TD></TR></TBODY></TABLE>");
		messageBody.append ("</TD></TR></TBODY></TABLE><BR>");
            
		// footer start
            
		messageBody.append ("<TABLE width='65%' border=0><TBODY>");
		messageBody.append ("<TR><TD>__________________________________________</TD></TR>");
		messageBody.append ("<TR><TD><FONT size=2>You are receiving this email because you have opted for it. <br>If you no longer wish");
		messageBody.append ("you receive this email. Please contact<br>the system administrator.</FONT></TD></TR>");
		messageBody.append ("<TR><TD><FONT size=2></FONT></TD></TR>");
		messageBody.append ("<TR><TD><FONT size=2></FONT></TD></TR>");
		messageBody.append ("</TBODY></TABLE><BR><BR></TD></TR></TBODY></TABLE>");
            
		// footer end
		messageBody.append ("</BLOCKQUOTE></BODY></HTML>");
            
		// html page design end
		return messageBody.toString();
	}
	
	public static void main( String agrs[] ) throws Exception{
		//System.setProperty( "cmd", "" );
		//System.setProperty( "scalar.properties", "c:/chiptrack/config/scalar.properties" );
		new TransitLotAlertJob().execute();
	}
}
